package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private ApartmentInfoService apartmentInfoService;


    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private AttrValueMapper attrValueMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;
    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private PaymentTypeMapper paymentTypeMapper;
    @Resource
    private LeaseTermMapper leaseTermMapper;
    @Resource
    private RoomInfoMapper roomInfoMapper;


    //保存或更新房间信息
    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        //1.优先判断是更新操作还是新增操作
        boolean isUpdate = roomSubmitVo.getId() != null;
        //2.不论是新增或是更新，都要执行这个公共操作saveOrUpdate
        this.saveOrUpdate(roomSubmitVo);
        //3.获取房间id
        Long roomId = roomSubmitVo.getId();
        //4.如果是新增操作，那么将不会执行下面的操作（删除中间表操作），如果是更新操作，那么执行下面的操作
        if (isUpdate){
            //5.更新传来的RoomSubmitVo有额外多的6条属性，对应是分别是图片，属性，配套，标签，支付方式以及可选租期列表
            // 其中除了图片列表，其他5个都涉及到中间表操作
            //TODO 4.1 根据房间id，删除图片列表
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.lambda()
                    .eq(GraphInfo::getItemType, ItemType.ROOM)
                    .eq(GraphInfo::getItemId,roomId);
            graphInfoService.remove(graphInfoQueryWrapper);
            //TODO 4.2 根据房间id，删除属性信息中间表
            QueryWrapper<RoomAttrValue> attrValueQueryWrapper = new QueryWrapper<>();
            attrValueQueryWrapper
                    .lambda()
                    .eq(RoomAttrValue::getRoomId,roomId);
            roomAttrValueService.remove(attrValueQueryWrapper);
            //TODO 4.3 根据房间id，删除配套信息中间表
            QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
            roomFacilityQueryWrapper
                    .lambda()
                    .eq(RoomFacility::getRoomId,roomId);
            roomFacilityService.remove(roomFacilityQueryWrapper);
            //TODO 4.4 根据房间id，删除标签信息中间表
            QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
            roomLabelQueryWrapper
                    .lambda()
                    .eq(RoomLabel::getRoomId,roomId);
            roomLabelService.remove(roomLabelQueryWrapper);
            //TODO 4.5 根据房间id，删除支付方式中间表
            QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
            roomPaymentTypeQueryWrapper
                    .lambda()
                    .eq(RoomPaymentType::getRoomId,roomId);
            roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);
            //TODO 4.6 根据房间id，删除可选租期中间表
            QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
            roomLeaseTermQueryWrapper
                    .lambda()
                    .eq(RoomLeaseTerm::getRoomId,roomId);
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);
        }
        //5.现在中间表信息都被删除了，下面开始添加新的数据
        //5.1 【图片部分】添加新的图片数据
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                BeanUtils.copyProperties(graphVo,graphInfo);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomId);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //5.2 【属性部分】添加新的属性数据到中间表中
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)){
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                                .attrValueId(attrValueId)
                                        .roomId(roomId)
                                                .build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
        //5.3 【配套部分】添加新的配套数据到中间表中
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder()
                        .facilityId(facilityInfoId)
                        .roomId(roomId)
                        .build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }
        //5.4 【标签部分】添加新的标签数据到中间表中
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)){
            List<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder()
                        .labelId(labelInfoId)
                        .roomId(roomId)
                        .build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        //5.5 【支付方式部分】添加新的支付方式数据到中间表中
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                        .paymentTypeId(paymentTypeId)
                        .roomId(roomId)
                        .build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //5.6 【可选租期部分】添加新的可选租期数据到中间表中
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                        .leaseTermId(leaseTermId)
                        .roomId(roomId)
                        .build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }
    }
    //根据id查询房间的详情信息
    @Override
    public RoomDetailVo findRoomDetailVoById(Long id) {
        //1.获取房间基本信息
        RoomInfo roomInfo = this.getById(id);
        //2.获取所属公寓的信息
        Long apartmentId = roomInfo.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(apartmentId);
        //3.获取图片信息列表
        List<GraphVo> graphVoList = graphInfoMapper.findListByItemTypeAndId(ItemType.ROOM, id);
        //4.获取属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.findListByRoomId(id);
        //5.获取配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findlistByRoomId(id);
        //6.获取标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.findListByRoomId(id);
        //7.获取支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.findListByRoomId(id);
        //8.获取可选租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.findListByRoomId(id);
        //9.组装RoomDetailVo对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }
    //根据id删除房间信息（包括中间表信息）
    @Override
    public void removeRoomById(Long roomId) {
        //1.根据房间id，删除图片列表
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.lambda()
                .eq(GraphInfo::getItemType, ItemType.ROOM)
                .eq(GraphInfo::getItemId, roomId);
        graphInfoService.remove(graphInfoQueryWrapper);
        //2.根据房间id，删除属性信息中间表
        QueryWrapper<RoomAttrValue> attrValueQueryWrapper = new QueryWrapper<>();
        attrValueQueryWrapper
                .lambda()
                .eq(RoomAttrValue::getRoomId, roomId);
        roomAttrValueService.remove(attrValueQueryWrapper);
        //3.根据房间id，删除配套信息中间表
        QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
        roomFacilityQueryWrapper
                .lambda()
                .eq(RoomFacility::getRoomId, roomId);
        roomFacilityService.remove(roomFacilityQueryWrapper);
        //4.根据房间id，删除标签信息中间表
        QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
        roomLabelQueryWrapper
                .lambda()
                .eq(RoomLabel::getRoomId, roomId);
        roomLabelService.remove(roomLabelQueryWrapper);
        //5.根据房间id，删除支付方式中间表
        QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
        roomPaymentTypeQueryWrapper
                .lambda()
                .eq(RoomPaymentType::getRoomId, roomId);
        roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);
        //6.根据房间id，删除可选租期中间表
        QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
        roomLeaseTermQueryWrapper
                .lambda()
                .eq(RoomLeaseTerm::getRoomId, roomId);
        roomLeaseTermService.remove(roomLeaseTermQueryWrapper);
        //7.最后，根据房间id，删除房间表中的数据
        this.removeById(roomId);
    }
    //根据条件分页查询房间列表
    @Override
    public IPage<RoomItemVo> findRoomItemVoPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.findRoomItemVoPage(page,queryVo);
    }
}




